
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>栅格数据创建与保存 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="读取HDF或者NetCDF格式的栅格数据.html" />
    
    
    <link rel="prev" href="栅格数据格式转换.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../Python基础/">
            
                <a href="../../Python基础/">
            
                    
                    Python基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../Python基础/Python环境搭建及基本数据类型.html">
            
                <a href="../../Python基础/Python环境搭建及基本数据类型.html">
            
                    
                    Python环境搭建及基本数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../Python基础/运算符及字符串.html">
            
                <a href="../../Python基础/运算符及字符串.html">
            
                    
                    运算符及字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../Python基础/序列与字典.html">
            
                <a href="../../Python基础/序列与字典.html">
            
                    
                    序列与字典
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../Python基础/流程控制语句.html">
            
                <a href="../../Python基础/流程控制语句.html">
            
                    
                    流程控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../Python基础/函数.html">
            
                <a href="../../Python基础/函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../Python基础/面向对象编程初识.html">
            
                <a href="../../Python基础/面向对象编程初识.html">
            
                    
                    面向对象编程初识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../Python基础/面向对象编程高级.html">
            
                <a href="../../Python基础/面向对象编程高级.html">
            
                    
                    面向对象编程高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../../Python基础/Python科学计算.html">
            
                <a href="../../Python基础/Python科学计算.html">
            
                    
                    Python科学计算
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../../Python基础/空间数据处理环境搭建.html">
            
                <a href="../../Python基础/空间数据处理环境搭建.html">
            
                    
                    空间数据处理环境搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../空间数据基础/">
            
                <a href="../../空间数据基础/">
            
                    
                    空间数据基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../空间数据基础/空间参考系统.html">
            
                <a href="../../空间数据基础/空间参考系统.html">
            
                    
                    空间参考系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../空间数据基础/地图投影.html">
            
                <a href="../../空间数据基础/地图投影.html">
            
                    
                    地图投影
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../空间数据基础/空间数据.html">
            
                <a href="../../空间数据基础/空间数据.html">
            
                    
                    空间数据
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../">
            
                <a href="../">
            
                    
                    GDAL空间数据处理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../GDAL简介.html">
            
                <a href="../GDAL简介.html">
            
                    
                    GDAL简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../GDAL数据基本操作.html">
            
                <a href="../GDAL数据基本操作.html">
            
                    
                    GDAL数据基本操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="打开栅格数据的正确方式.html">
            
                <a href="打开栅格数据的正确方式.html">
            
                    
                    打开栅格数据的正确方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="栅格数据格式转换.html">
            
                <a href="栅格数据格式转换.html">
            
                    
                    栅格数据格式转换
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2.3" data-path="栅格数据创建与保存.html">
            
                <a href="栅格数据创建与保存.html">
            
                    
                    栅格数据创建与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.4" data-path="读取HDF或者NetCDF格式的栅格数据.html">
            
                <a href="读取HDF或者NetCDF格式的栅格数据.html">
            
                    
                    读取HDF或者NetCDF格式的栅格数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.5" data-path="栅格数据投影转换.html">
            
                <a href="栅格数据投影转换.html">
            
                    
                    栅格数据投影转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.6" data-path="栅格数据裁剪.html">
            
                <a href="栅格数据裁剪.html">
            
                    
                    栅格数据裁剪
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.7" data-path="打开Shapefile文件的正确方式.html">
            
                <a href="打开Shapefile文件的正确方式.html">
            
                    
                    打开Shapefile文件的正确方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.8" data-path="创建Shapefile文件并写入数据.html">
            
                <a href="创建Shapefile文件并写入数据.html">
            
                    
                    创建Shapefile文件并写入数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.9" data-path="矢量数据投影转换.html">
            
                <a href="矢量数据投影转换.html">
            
                    
                    矢量数据投影转换
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../Fiona矢量数据处理/REDAME.md">
            
                <span>
            
                    
                    Fiona矢量数据处理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.3.1" data-path="../Fiona矢量数据处理/Fiona简介及Shapefile数据读取.html">
            
                <a href="../Fiona矢量数据处理/Fiona简介及Shapefile数据读取.html">
            
                    
                    Fiona简介及Shapefile数据读取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3.2" data-path="../Fiona矢量数据处理/使用Fiona创建Shapefile矢量数据.html">
            
                <a href="../Fiona矢量数据处理/使用Fiona创建Shapefile矢量数据.html">
            
                    
                    使用Fiona创建Shapefile矢量数据
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../Rasterio栅格数据处理/README.md">
            
                <span>
            
                    
                    Rasterio栅格数据处理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.4.1" data-path="../Rasterio栅格数据处理/使用Rasterio读取栅格数据.html">
            
                <a href="../Rasterio栅格数据处理/使用Rasterio读取栅格数据.html">
            
                    
                    使用Rasterio读取栅格数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4.2" data-path="../Rasterio栅格数据处理/使用Rasterio创建栅格数据.html">
            
                <a href="../Rasterio栅格数据处理/使用Rasterio创建栅格数据.html">
            
                    
                    使用Rasterio创建栅格数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4.3" data-path="../Rasterio栅格数据处理/使用Rasterio做投影变换.html">
            
                <a href="../Rasterio栅格数据处理/使用Rasterio做投影变换.html">
            
                    
                    使用Rasterio做投影变换
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >栅格数据创建与保存</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6805;&#x683C;&#x6570;&#x636E;&#x521B;&#x5EFA;&#x4E0E;&#x4FDD;&#x5B58;">&#x6805;&#x683C;&#x6570;&#x636E;&#x521B;&#x5EFA;&#x4E0E;&#x4FDD;&#x5B58;</h1>
<p>&#x4F5C;&#x8005;&#xFF1A;&#x963F;&#x632F;</p>
<p>&#x90AE;&#x7BB1;&#xFF1A;tanzhenyugis@163.com</p>
<p>&#x535A;&#x5BA2;&#xFF1A;<a href="https://blog.csdn.net/theonegis/article/details/80089375" target="_blank">https://blog.csdn.net/theonegis/article/details/80089375</a></p>
<p>&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#xFF1A;2018-05-24</p>
<p>&#x58F0;&#x660E;&#xFF1A;&#x672C;&#x6587;&#x4E3A;&#x535A;&#x4E3B;&#x539F;&#x521B;&#x6587;&#x7AE0;&#xFF0C;&#x8F6C;&#x8F7D;&#x8BF7;&#x6CE8;&#x660E;&#x539F;&#x6587;&#x51FA;&#x5904;</p>
<hr>
<h2 id="&#x601D;&#x8DEF;&#x4E0E;&#x65B9;&#x6CD5;">&#x601D;&#x8DEF;&#x4E0E;&#x65B9;&#x6CD5;</h2>
<p>&#x4F7F;&#x7528;Python&#x8FDB;&#x884C;&#x6805;&#x683C;&#x6570;&#x636E;&#x5904;&#x7406;&#xFF0C;&#x5F88;&#x591A;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5C06;GDAL&#x7684;<code>Dataset</code>&#x5BF9;&#x8C61;&#x8F6C;&#x5316;&#x4E3A;NumPy&#x7684;<code>ndarray</code>&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5F88;&#x591A;&#x901A;&#x7528;&#x7684;Python&#x5E93;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x501F;&#x52A9;GDAL&#x5E93;&#x5C06;&#x6570;&#x636E;&#x5199;&#x56DE;&#x5230;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x4E0D;&#x540C;&#x4E8E;&#x666E;&#x901A;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#xFF0C;&#x7A7A;&#x95F4;&#x6805;&#x683C;&#x6570;&#x636E;&#x7684;&#x5199;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E24;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x6570;&#x636E;&#x7684;&#x6295;&#x5F71;&#x4FE1;&#x606F;&#xFF08;&#x786E;&#x5B9A;&#x4E86;&#x5E73;&#x9762;&#x5750;&#x6807;&#x7CFB;&#xFF09;</li>
<li>&#x6570;&#x636E;&#x7684;&#x5730;&#x7406;&#x5750;&#x6807;&#x4FE1;&#x606F;&#xFF08;&#x786E;&#x5B9A;&#x4E86;&#x56FE;&#x50CF;&#x5728;&#x7ED9;&#x5B9A;&#x5750;&#x6807;&#x7CFB;&#x4E0B;&#x7684;&#x4F4D;&#x7F6E;&#xFF09;</li>
</ol>
<p>&#x5728;GDAL&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x9700;&#x8981;&#x521B;&#x5EFA;<code>Dataset</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x7ED9;<code>Dataset</code>&#x5BF9;&#x8C61;&#x586B;&#x5145;&#x6570;&#x636E;&#x4EE5;&#x53CA;&#x5143;&#x6570;&#x636E;&#x4FE1;&#x606F;&#x5C31;OK&#x4E86;&#x3002;</p>
<p><code>Driver</code>&#x6216;&#x8005;&#x8BF4;<code>GDALDriver</code>&#xFF08;Python&#x7248;&#x672C;&#x7684;API&#x4E2D;&#x5BF9;&#x8C61;&#x540D;&#x79F0;&#x597D;&#x50CF;&#x90FD;&#x53BB;&#x6389;&#x4E86;&#x524D;&#x7F00;GDAL&#xFF0C;&#x800C;C/C++&#x7248;&#x672C;&#x7684;API&#x5F88;&#x591A;&#x5BF9;&#x8C61;&#x524D;&#x9762;&#x90FD;&#x662F;&#x6709;GDAL&#x524D;&#x7F00;&#x7684;&#xFF0C;&#x5982;GDALDataset&#x5BF9;&#x8C61;&#x5728;Python&#x4E2D;&#x5BF9;&#x5E94;&#x7684;&#x662F;Dataset&#x5BF9;&#x8C61;&#xFF09;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;<code>Create()</code>&#x548C;<code>CreateCopy()</code></p>
<p>&#x6240;&#x4EE5;&#xFF0C;&#x76F8;&#x5E94;&#x5730;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x6709;&#x4E24;&#x79CD;&#x601D;&#x8DEF;&#x53BB;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>Dataset</code>&#x5BF9;&#x8C61;&#xFF1A;</p>
<ol>
<li>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x6570;&#x636E;&#xFF0C;&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x5BF9;&#x539F;&#x59CB;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x4E86;&#x5904;&#x7406;&#xFF0C;&#x5904;&#x7406;&#x4E4B;&#x540E;&#xFF0C;&#x7A7A;&#x95F4;&#x4FE1;&#x606F;&#xFF0C;&#x6CE2;&#x6BB5;&#x7B49;&#x90FD;&#x6CA1;&#x6709;&#x53D8;&#x5316;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x5C06;&#x539F;&#x59CB;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x539F;&#x578B;&#x6570;&#x636E;&#xFF0C;&#x4F7F;&#x7528;<code>CreateCopy()</code>&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x548C;&#x539F;&#x59CB;&#x6570;&#x636E;&#x4E00;&#x6837;&#x7684;<code>Dataset</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x521B;&#x5EFA;&#x597D;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#x586B;&#x5145;&#x4E00;&#x4E2A;<code>ndarray</code>&#x6570;&#x636E;&#x5C31;&#x597D;&#x4E86;&#x3002;</li>
<li>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x6570;&#x636E;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>Create()</code>&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;<code>Dataset</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x624B;&#x52A8;&#x8BBE;&#x7F6E;&#x5BF9;&#x8C61;&#x7684;&#x6CE2;&#x6BB5;&#xFF0C;&#x5C3A;&#x5BF8;&#xFF0C;&#x7A7A;&#x95F4;&#x4FE1;&#x606F;&#x7B49;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5728;&#x5BF9;&#x5E94;&#x7684;&#x6CE2;&#x6BB5;&#x586B;&#x7A7A;<code>ndarray</code>&#x5177;&#x4F53;&#x7684;&#x6570;&#x636E;&#x3002;</li>
</ol>
<h2 id="&#x5B9E;&#x73B0;&#x51FD;&#x6570;">&#x5B9E;&#x73B0;&#x51FD;&#x6570;</h2>
<p>&#x6211;&#x628A;&#x4E0A;&#x9762;&#x4E24;&#x79CD;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#x7F16;&#x7801;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-Python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">array2raster</span><span class="hljs-params">(f_name, np_array, driver=<span class="hljs-string">&apos;GTiff&apos;</span>,
                 prototype=None,
                 xsize=None, ysize=None,
                 transform=None, projection=None,
                 dtype=None, nodata=None)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x5C06;ndarray&#x6570;&#x7EC4;&#x5199;&#x5165;&#x5230;&#x6587;&#x4EF6;&#x4E2D;
    :param f_name: &#x6587;&#x4EF6;&#x8DEF;&#x5F84;
    :param np_array: ndarray&#x6570;&#x7EC4;
    :param driver: &#x6587;&#x4EF6;&#x683C;&#x5F0F;&#x9A71;&#x52A8;
    :param prototype: &#x6587;&#x4EF6;&#x539F;&#x578B;
    :param xsize: &#x56FE;&#x50CF;&#x7684;&#x5217;&#x6570;
    :param ysize: &#x56FE;&#x50CF;&#x7684;&#x884C;&#x6570;
    :param transform: GDAL&#x4E2D;&#x7684;&#x7A7A;&#x95F4;&#x8F6C;&#x6362;&#x516D;&#x53C2;&#x6570;
    :param projection: &#x6570;&#x636E;&#x7684;&#x6295;&#x5F71;&#x4FE1;&#x606F;
    :param dtype: &#x6570;&#x636E;&#x5B58;&#x50A8;&#x7684;&#x7C7B;&#x578B;
    :param nodata: NoData&#x5143;&#x6570;&#x636E;
    &quot;&quot;&quot;</span>
    <span class="hljs-comment"># &#x521B;&#x5EFA;&#x8981;&#x5199;&#x5165;&#x7684;&#x6570;&#x636E;&#x96C6;&#xFF08;&#x8FD9;&#x91CC;&#x5047;&#x8BBE;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6CE2;&#x6BB5;&#xFF09;</span>
    <span class="hljs-comment"># &#x5206;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;&#x4E00;&#x79CD;&#x7ED9;&#x5B9A;&#x4E86;&#x6570;&#x636E;&#x539F;&#x578B;&#xFF0C;&#x4E00;&#x79CD;&#x6CA1;&#x6709;&#x7ED9;&#x5B9A;&#xFF0C;&#x9700;&#x8981;&#x624B;&#x52A8;&#x6307;&#x5B9A;Transform&#x548C;Projection</span>
    driver = gdal.GetDriverByName(driver)
    <span class="hljs-keyword">if</span> prototype:
        dataset = driver.CreateCopy(f_name, prototype)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">if</span> dtype <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
            dtype = gdal.GDT_Float32
        <span class="hljs-keyword">if</span> xsize <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
            xsize = np_array.shape[<span class="hljs-number">-1</span>]  <span class="hljs-comment"># &#x6570;&#x7EC4;&#x7684;&#x5217;&#x6570;</span>
        <span class="hljs-keyword">if</span> ysize <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
            ysize = np_array.shape[<span class="hljs-number">-2</span>]  <span class="hljs-comment"># &#x6570;&#x7EC4;&#x7684;&#x884C;&#x6570;</span>
        dataset = driver.Create(f_name, xsize, ysize, <span class="hljs-number">1</span>, dtype)  <span class="hljs-comment"># &#x8FD9;&#x91CC;&#x7684;1&#x6307;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x6CE2;&#x6BB5;</span>
        dataset.SetGeoTransform(transform)
        dataset.SetProjection(projection)
    <span class="hljs-comment"># &#x5C06;array&#x5199;&#x5165;&#x6587;&#x4EF6;</span>
    dataset.GetRasterBand(<span class="hljs-number">1</span>).WriteArray(np_array)
    <span class="hljs-keyword">if</span> nodata <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>:
        dataset.GetRasterBand(<span class="hljs-number">1</span>).SetNoDataValue(nodata)
    dataset.FlushCache()
    <span class="hljs-keyword">return</span> f_name
</code></pre>
<p>&#x5728;&#x4F7F;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8981;&#x4E48;&#x4F20;&#x8FDB;&#x53BB;&#x4E00;&#x4E2A;<code>prototype</code>&#x539F;&#x578B;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x8981;&#x4E48;&#x4F20;&#x8FDB;&#x53BB;<code>transform</code>&#x548C;<code>projection</code>&#x7B49;&#x4FE1;&#x606F;&#xFF0C;&#x8FD9;&#x6837;&#x5199;&#x5165;&#x7684;&#x6587;&#x4EF6;&#x624D;&#x5177;&#x6709;&#x7A7A;&#x95F4;&#x53C2;&#x8003;&#x3002;</p>
<h2 id="&#x6D4B;&#x8BD5;&#x6848;&#x4F8B;">&#x6D4B;&#x8BD5;&#x6848;&#x4F8B;</h2>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x8BA1;&#x7B97;NDVI&#xFF08;Normalized Difference Vegetation Index&#xFF0C;&#x5F52;&#x4E00;&#x5316;&#x690D;&#x88AB;&#x6307;&#x6570;&#xFF09;&#x548C;DVI&#xFF08;Difference Vegetation Index&#xFF0C;&#x5DEE;&#x503C;&#x690D;&#x88AB;&#x6307;&#x6570;&#xFF09;&#x7684;&#x4F8B;&#x5B50;&#x3002;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x8BA1;&#x7B97;NDVI&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;&#x4ECE;&#x539F;&#x59CB;&#x6570;&#x636E;&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#x7A7A;&#x95F4;&#x6295;&#x5F71;&#x548C;&#x7A7A;&#x95F4;&#x53D8;&#x6362;&#x516D;&#x5143;&#x7EC4;&#x4FE1;&#x606F;&#x521B;&#x5EFA;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#xFF1B;&#x7136;&#x540E;&#x518D;&#x8BA1;&#x7B97;DVI&#xFF0C;&#x901A;&#x8FC7;NDVI&#x6587;&#x4EF6;&#x4F5C;&#x4E3A;&#x539F;&#x578B;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x4EE5;&#x521B;&#x5EFA;DVI&#x7684;&#x8F93;&#x51FA;&#x6570;&#x636E;&#x96C6;&#x3002;</p>
<p>&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-Python"><span class="hljs-comment"># &#x6253;&#x5F00;&#x6805;&#x683C;&#x6570;&#x636E;&#x96C6;</span>
ds = gdal.Open(<span class="hljs-string">&apos;example.tif&apos;</span>) <span class="hljs-comment"># example.tif&#x6709;&#x4E09;&#x4E2A;&#x6CE2;&#x6BB5;&#xFF0C;&#x5206;&#x522B;&#x662F;&#x84DD;&#xFF0C;&#x7EA2;&#xFF0C;&#x8FD1;&#x7EA2;&#x5916;</span>

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x6570;&#x636E;&#x96C6;&#x7684;&#x4E00;&#x4E9B;&#x4FE1;&#x606F;</span>
x_size = ds.RasterXSize  <span class="hljs-comment"># &#x56FE;&#x50CF;&#x5217;&#x6570;</span>
y_size = ds.RasterYSize  <span class="hljs-comment"># &#x56FE;&#x50CF;&#x884C;&#x6570;</span>

proj = ds.GetProjection()  <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x7684;&#x662F;WKT&#x683C;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;</span>
trans = ds.GetGeoTransform()  <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x516D;&#x4E2A;&#x53C2;&#x6570;&#x7684;tuple</span>

<span class="hljs-comment"># &#x5728;&#x6570;&#x636E;&#x96C6;&#x5C42;&#x9762;ReadAsArray&#x65B9;&#x6CD5;&#x5C06;&#x6BCF;&#x4E2A;&#x6CE2;&#x6BB5;&#x90FD;&#x8F6C;&#x6362;&#x4E3A;&#x4E86;&#x4E00;&#x4E2A;&#x4E8C;&#x7EF4;&#x6570;&#x7EC4;</span>
image = ds.ReadAsArray()

<span class="hljs-comment"># &#x83B7;&#x5F97;&#x6CE2;&#x6BB5;&#x5BF9;&#x5E94;&#x7684;array</span>
bnd_red = image[<span class="hljs-number">1</span>].astype(float)  <span class="hljs-comment"># &#x7EA2;&#x6CE2;&#x6BB5;</span>
bnd_nir = image[<span class="hljs-number">2</span>].astype(float)  <span class="hljs-comment"># &#x8FD1;&#x7EA2;&#x5916;&#x6CE2;&#x6BB5;</span>

idx_ndvi = (bnd_nir - bnd_red) / (bnd_nir + bnd_red)  <span class="hljs-comment"># &#x8BA1;&#x7B97;NDVI&#x6307;&#x6570;</span>

out1_file = <span class="hljs-string">&apos;NDVI.tif&apos;</span>
array2raster(out1_file, idx_ndvi,
             xsize=x_size, ysize=y_size,
             transform=trans, projection=proj,
             dtype=gdal.GDT_Float32)

idx_dvi = bnd_nir - bnd_red  <span class="hljs-comment"># &#x8BA1;&#x7B97;DVI&#x6307;&#x6570;</span>

out2_file = <span class="hljs-string">&apos;DVI.tif&apos;</span>
<span class="hljs-comment"># &#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4F7F;&#x7528;out1_file&#x4F5C;&#x4E3A;&#x539F;&#x578B;&#x56FE;&#x50CF;&#x4F5C;&#x4E3A;&#x53C2;&#x8003;&#x6765;&#x4FDD;&#x5B58;out2_file</span>
array2raster(out2_file, idx_ndvi, prototype=gdal.Open(out1_file))

<span class="hljs-comment"># &#x5173;&#x95ED;&#x6570;&#x636E;&#x96C6;</span>
ds = <span class="hljs-keyword">None</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="栅格数据格式转换.html" class="navigation navigation-prev " aria-label="Previous page: 栅格数据格式转换">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="读取HDF或者NetCDF格式的栅格数据.html" class="navigation navigation-next " aria-label="Next page: 读取HDF或者NetCDF格式的栅格数据">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"栅格数据创建与保存","level":"1.4.2.3","depth":3,"next":{"title":"读取HDF或者NetCDF格式的栅格数据","level":"1.4.2.4","depth":3,"path":"GDAL空间数据处理/GDAL数据基本操作/读取HDF或者NetCDF格式的栅格数据.md","ref":"GDAL空间数据处理/GDAL数据基本操作/读取HDF或者NetCDF格式的栅格数据.md","articles":[]},"previous":{"title":"栅格数据格式转换","level":"1.4.2.2","depth":3,"path":"GDAL空间数据处理/GDAL数据基本操作/栅格数据格式转换.md","ref":"GDAL空间数据处理/GDAL数据基本操作/栅格数据格式转换.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"GDAL空间数据处理/GDAL数据基本操作/栅格数据创建与保存.md","mtime":"2019-03-22T18:52:30.713Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-03-22T20:04:12.549Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

